﻿Imports System.Text.RegularExpressions

''' <summary>
''' Represents a single word parsed out of a command line string.
''' </summary>
''' <remarks></remarks>
Public Structure CommandLineWord
    Public ReadOnly Word As String
    Public ReadOnly LineIndex As Integer
    Public ReadOnly WordIndex As Integer

    Public Sub New(wordMatch As Match, index As Integer)
        Word = wordMatch.Captures(0).Value
        LineIndex = wordMatch.Index
        WordIndex = index
    End Sub

    Public Overrides Function Equals(obj As Object) As Boolean
        If obj IsNot Nothing Then
            If TypeOf obj Is CommandLineWord Then
                Return Word.Equals(CType(obj, CommandLineWord).Word)
            End If
            Return Word.Equals(obj.ToString)
        End If
        Return False
    End Function

    Public Overrides Function GetHashCode() As Integer
        Return Word.GetHashCode
    End Function

    Public Function ToKeyValuePair() As KeyValuePair(Of String, String)
        If Word.Contains("=") Then
            Dim values() As String = Word.Split("="c)
            Return New KeyValuePair(Of String, String)(values(0), values(1))
        End If
        Return Nothing
    End Function

    Public Overrides Function ToString() As String
        Return Word
    End Function

    Public Shared Narrowing Operator CType(target As CommandLineWord) As String
        Return target.Word
    End Operator
End Structure